#include "MainWindow.h"

#include <QMessageBox>

#include "WordsSolitaire.h"
#include "ShowAndGuess.h"
#include "MenuBar.h"

MainWindow::MainWindow()
{
    // 设置窗口大小和标题
    setFixedSize(800, 600);
    setWindowTitle("单词输入与展示");

    menuBar = new MenuBar();
    setMenuBar(menuBar);

    connect(menuBar->games->wordsSolitaire, &QAction::triggered, this, &MainWindow::onWordsSolitaireTriggered);
    connect(menuBar->games->showAndGuess, &QAction::triggered, this, &MainWindow::onShowAndGuessTriggered);

    connect(menuBar->window->close, &QAction::triggered, this, &MainWindow::close);
    connect(menuBar->help->about, &QAction::triggered, this, &MainWindow::onAboutTriggered);    

    wordsSolitaire = new WordsSolitaire();

    // 将中心部件设置给主窗口
    setCentralWidget(wordsSolitaire);
}

void  MainWindow::onWordsSolitaireTriggered() {
    if(!wordsSolitaire)
        wordsSolitaire = new WordsSolitaire();
    setCentralWidget(wordsSolitaire);
    if(showAndGuess!= nullptr) {
        delete showAndGuess;
        showAndGuess = nullptr;
    }
}

void MainWindow::onShowAndGuessTriggered() {
    if(showAndGuess == nullptr)
        showAndGuess = new ShowAndGuess();
    setCentralWidget(showAndGuess);
    if(wordsSolitaire != nullptr) {
        delete wordsSolitaire;
        wordsSolitaire = nullptr;
    }
}   

void MainWindow::onAboutTriggered() {
    QMessageBox::about(this,"作者信息","作者：Torres Liang");
}